package com.example.linphone_flutter;

import org.linphone.core.Address;
import org.linphone.core.ChatMessage;
import org.linphone.core.ChatParams;
import org.linphone.core.ChatRoom;
import org.linphone.core.ChatRoomParams;
import org.linphone.core.Conference;
import org.linphone.core.ConferenceParams;
import org.linphone.core.Core;
import org.linphone.core.Factory;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class ChatMessageManager {

    private Core core;
    private BinaryMessenger messenger;
    public static ChatRoom chatRoom;
    private ChatMessageListener chatMessageListener;
    public ChatMessageManager(Core core, BinaryMessenger messenger) {
        this.core = core;
        this.messenger = messenger;
        chatMessageListener = new ChatMessageListener(messenger);
    }


    void createBasicChatRoom(String remoteSipUri) {
        if(core.getDefaultAccount()==null){
            return;
        }
        // In this tutorial we will create a Basic chat room
        // It doesn't include advanced features such as end-to-end encryption or groups
        // But it is interoperable with any SIP service as it's relying on SIP SIMPLE messages

        // First let's create conference params (as a chat room is a conference with a text stream)
        ConferenceParams params = core.createConferenceParams(null);
        params.setChatEnabled(true);
        params.setGroupEnabled(false);
        params.setAccount(core.getDefaultAccount());
        params.setSecurityLevel(Conference.SecurityLevel.PointToPoint);

        // Now let's configure the chat part
        ChatParams chatParams = params.getChatParams();
        if(chatParams==null){
            return;
        }
        chatParams.setEphemeralLifetime(0);
        chatParams.setBackend(ChatRoom.Backend.Basic);

        if (params.isValid()) {
            // We also need the SIP address of the person we will chat with
            Address remoteAddress = Factory.instance().createAddress(remoteSipUri);
            if (remoteAddress != null) {
                ChatRoom room = core.createChatRoom(params, new Address[]{remoteAddress});
                if (room != null) {
                    chatRoom = room;
                }
            }
        }
    }

    String sendMessage(MethodCall call, MethodChannel.Result result) {
        String remoteSipUri = call.argument("remoteSipUri");
        String message = call.argument("message");
        String localTime = call.argument("localTime");
        if (chatRoom == null) {
            createBasicChatRoom(remoteSipUri);
        }
        if (chatRoom != null) {
            ChatMessage chatMessage = chatRoom.createMessageFromUtf8(message);
            chatMessage.setUserData(localTime);
            chatMessage.addListener(chatMessageListener);
            chatMessage.send();
        }
        result.success(localTime);
        return "";
    }

    void sendImage() {
    }


    void copy(String from, String to) {

    }

    void destroy(){
        chatRoom = null;
    }

}


